package com.keyancompany.zhongbaopingtai.activity;

import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.EventBusEntity;
import com.keyancompany.zhongbaopingtai.model.Weekly;
import com.keyancompany.zhongbaopingtai.utils.DateUtils;
import com.keyancompany.zhongbaopingtai.utils.T;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

/**
 * 发布周报
 * <p>
 * Created by firefox on 2016/6/17.
 */
public class CreateWeeklyActivity extends BaseActivity {

    @BindString(R.string.publish_weekly_text)
    String title;

    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.weekly_content)
    EditText weeklyContent;

    @Bind(R.id.weekly_time)
    TextView weeklyTime;

    @Bind(R.id.submit_weekly)
    TextView submitWeekly;
    private String teamCode;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_create_weekly;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {

        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {
        weeklyTime.setText(DateUtils.CurrentTime());
        teamCode = getIntent().getStringExtra(Constants.TEAM_CODE);
    }


    @OnClick(R.id.submit_weekly)
    public void onClick() {
        if (isNetvaiable()) {
            String content = weeklyContent.getText().toString();
            if (TextUtils.isEmpty(content)) {
                T.showShort(mContext, "请输入周报内容！");
                return;
            }
            TeamAPI.saveWeekly(teamCode, content, DateUtils.CurrentTime() + "", CreateWeeklyActivity.this,
                    getLoading(R.string.watting_hint));
        }
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        BaseModel base = validateJson(json);
        if (base != null && base.isSuccess()) {
            EventBus.getDefault().post(new EventBusEntity(EventBusEntity.Type.REFRENCE_WEEKLY));
            finish();
        }
        T.showShort(mContext, base.msg);
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        T.showShort(mContext, error);
    }
}
